<?php

/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */
class omeanalysts_ctl_analysis_mxreport extends desktop_controller
{

    var $workground = 'omeanalysts_ctl_analysis_mxreport';
    var $pagelimit = 10;

    public function __construct($app)
    {
        parent::__construct($app);
        header("cache-control: no-store, no-cache, must-revalidate");
    }

    function index($params = null, $page = 1)
    {
        $where = array('active' => 'true');
        if (!empty($this->shopIds)) {
            $shopIds = $this->shopIds;
            $where['shop_id'] = $this->shopIds;
        } else {
            $shopIds = "";
        }
        set_time_limit(600);

        $this->workground = 'invoice_center';

        $params = array_merge($_POST, $_GET);
//        付款时间
        $params['begin_date'] = $params['begin_date'] ? $params['begin_date'] : date("Y-m-d", time());
        $params['end_date'] = $params['end_date'] ? $params['end_date'] : date("Y-m-d", time());
        if ($params['begin_date']) {
            $params['begin_date'] = strtotime($params['begin_date']);
            $params['begin_date'] = date("Y-m-d", $params['begin_date']);
        }
        if ($params['end_date']) {
            $params['end_date'] = strtotime($params['end_date']);
            $params['end_date'] = date("Y-m-d", $params['end_date']);
        }
        $params['shop_id'] = $params['shop_id'] ? $params['shop_id'] : $shopIds;
        $params['data_type'] = $params['data_type'] ? $params['data_type'] : 'pay';
        $params['materiaBn'] = $params['materiaBn'];

        unset($params['p']);
        unset($params['_DTYPE_DATE']);
        unset($params['app']);
        unset($params['ctl']);
        unset($params['act']);

        $branch_mdl = app::get("ome")->model("shop");
        $branch_datas = $branch_mdl->getList("shop_id,name", $where);
        foreach ($branch_datas as $v) {
            $branch_data[$v['shop_id']] = $v['name'];
        }

        $mxObj = app::get("wms")->model("orderform");
        $deliverycomp_obj = app::get('wms')->model('deliverycomp');

        //进行分页处理
        $pagelimit = 500;
        $getcount = $mxObj->getCount($params);
        $count = $getcount[0]['_count'];
//       $count = count($this->pagedata['datas']);
        $params['limit_beg'] = $pagelimit * ($page - 1);
        $params['limit_end'] = $pagelimit;
        $this->pagedata['limit_beg'] = $params['limit_beg'];
        $this->pagedata['limit_end'] = $params['limit_end'];

        //获取客户名称 group by
        $customerList = $deliverycomp_obj->getCustomerList($this->shopIds);
        $this->pagedata['customerList'] = $customerList;
        $this->pagedata['datas'] = $mxObj->getMxReport($params);


        $this->pagedata['shop_data'] = $branch_data;
        $this->pagedata['clearingtable'] = $params;
        $this->pagedata['materiaBn'] = $params['materiaBn'];
        $params = http_build_query($params);
        $ui = new base_component_ui($this->app);

        $params['use_buildin_export'] = true;
        $pager = $ui->pager(array(
            'current' => $page,
            'total' => ceil($count / $pagelimit),
            'link' => 'index.php?app=omeanalysts&ctl=analysis_mxreport&act=index&p[0]=123&p[1]=%s' . '&' . $params,
            'token' => time(),
        ));
        $this->pagedata['pager'] = $pager;
//        echo "<pre>";
//        print_r($this->pagedata['pager']);
//        echo "</pre>";exit;
        $this->page('analysis/mxreport/reportshow.html');
//        $this->finder('omeanalysts_mdl_test', $params);
    }

    function report()
    {
        $where = array('active' => 'true');
        if (!empty($this->shopIds)) {
            $shopIds = $this->shopIds;
            $where['shop_id'] = $this->shopIds;
        } else {
            $shopIds = "";
        }

        set_time_limit(600);
        //print_r($_GET);exit;
        $this->workground = 'invoice_center';
//        付款时间
        $_GET['begin_date'] = $_GET['begin_date'] ? $_GET['begin_date'] : date("Y-m-d", time());
        $_GET['end_date'] = $_GET['end_date'] ? $_GET['end_date'] : date("Y-m-d", time());
        if ($_GET['begin_date']) {
            $_GET['begin_date'] = strtotime($_GET['begin_date']);
            $_GET['begin_date'] = date("Y-m-d", $_GET['begin_date']);
        }
        if ($_GET['end_date']) {
            $_GET['end_date'] = strtotime($_GET['end_date']);
            $_GET['end_date'] = date("Y-m-d", $_GET['end_date']);
        }

        $_GET['shop_id'] = $_GET['shop_id'] ? $_GET['shop_id'] : $shopIds;
        $_GET['data_type'] = $_GET['data_type'] ? $_GET['data_type'] : 'pay';

        $branch_mdl = app::get("ome")->model("shop");
        $branch_datas = $branch_mdl->getList("shop_id,name", $where);
        foreach ($branch_datas as $v) {
            $branch_data[$v['shop_id']] = $v['name'];
        }
        $mxObj = app::get("wms")->model("orderform");
        $datas = $mxObj->getMxReport($_GET);
        include_once ROOT_DIR . "/custom/lib/phpExcel.php";

        $excel = new phpExcel();
        $excel->addHeader(array(
            '店铺名称', '订单号', '货号', '宝贝名称', '进度状态', '单位', '订单数量', '发货数量', '下单时间', '订单发货时间', '备货数量', '备货时间',
            '订单支付状态', '订单销售价', '订单原价', '配送费用', '保价费用', '税金', '折扣', '商品促销优惠', '整笔订单促销优惠', '优惠后订单金额', '订单已退款金额',
            '商品优惠分摊', '整笔订单优惠分摊', '折扣费用分摊', '配送费用分摊',
            '保价费用分摊', '税金分摊', '分摊汇总',
            '优惠后发货金额', '优惠后发货单价', '货品原价', '发货配送费用',
            'AX销售订单号', 'AX送货单号', '快递单号', '快递公司', '退款确认时间', '退款数量', '退款金额', '备注', '地区', '地址'
        ));
        if ($datas) {
            $data_xls = array();
            foreach ($datas as $val) {
                $data_xls[] = array(
                    $val['shop_name'],
                    ":@" . $val['order_bn'],
                    $val['order_items_bn'],
                    $val['order_items_name'],
                    $val['status'],
                    $val['unit'],
                    $val['order_items_nums'],
                    $val['delivery_detail_number'],
                    $val['paytime'],
                    $val['ax_time'],
                    $val['delivery_detail_number'],
                    $val['create_time'],
                    $val['pay_status'],
                    $val['total_amount'],
                    $val['cost_item'],
                    $val['cost_freight'],
                    $val['cost_protect'],
                    $val['cost_tax'],
                    $val['discount'],
                    $val['pmt_goods'],
                    $val['pmt_order'],
                    $val['payed'],
                    $val['refunds_money'],
                    $val['order_items_pmt_price_single'],
                    $val['order_items_pmt_bn_sigle'],
                    $val['order_items_discount_bn_sigle'],
                    $val['order_items_cost_freight_bn_sigle'],
                    $val['order_items_cost_protect_single'],
                    $val['order_items_cost_tax_single'],
                    $val['order_items_pmt_single'],
                    $val['delivery_detail_pmt_amount'],
                    $val['delivery_detail_pmt_price'],
                    $val['order_items_price'],
                    $val['delivery_detail_cost_freight'],
                    $val['axorderno'],
                    $val['axshipno'],
                    ":@" . $val['logi_no'],
                    $val['logi_name'],
                    $val['t_ready'],
                    $val['t_count'],
                    $val['t_money'],
                    $val['remark'],
                    $val['ship_area'],
                    $val['ship_addr'],
                );
            }
        }
        $excel->addBody(
            $data_xls
        );
        $excel->downLoad();
        exit;
    }

}
